using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace irc_core
{
    class xmlconfig
    {
        private XmlDocument __xml = new XmlDocument();
        private XmlElement _xml;
        public object[] _o_users;
        public object[] _o_channels;
        

        public xmlconfig(string xmlfilename)
        {
            __xml.Load(xmlfilename);
            _xml = __xml["IRC-CORE"];
            this._o_users = this.Users();
            this._o_channels = this.Channels();

        }

        public XmlElement XmlRoot()
        {
            return _xml;
        }

        public string Host()
        { return this.XmlRoot()["CONFIG"]["SERVER"].Attributes["HOST"].Value; }

        public string Port()
        { return this.XmlRoot()["CONFIG"]["SERVER"].Attributes["PORT"].Value; }

        public string NickName()
        { return this.XmlRoot()["CONFIG"]["BOT"].Attributes["NICKNAME"].Value; }

        public string AltNickName()
        { return this.XmlRoot()["CONFIG"]["BOT"].Attributes["ALTNICKNAME"].Value; }

        public object[] Users()
        { 
            object[] _o_lusers = new object[this.XmlRoot()["USERS"].ChildNodes.Count];
            object[] _o_user = new object[5];
            for (int i = 0; i < this.XmlRoot()["USERS"].ChildNodes.Count; i++)
            {
                _o_user[0] = (object)this.XmlRoot()["USERS"].ChildNodes[i].Attributes["NICKNAME"].Value;
                _o_user[1] = (object)this.XmlRoot()["USERS"].ChildNodes[i].Attributes["HOSTMASK"].Value;
                _o_user[2] = (object)this.XmlRoot()["USERS"].ChildNodes[i].Attributes["PASSWORD"].Value;
                _o_user[3] = (object)this.XmlRoot()["USERS"].ChildNodes[i].Attributes["AUTH"].Value;
                _o_user[4] = (object)this.XmlRoot()["USERS"].ChildNodes[i].Attributes["PERMISSION"].Value;

                _o_lusers[i] = (object)_o_user;
            }
            
            return _o_lusers; 
        
        }


        public object[] Channels()
        {
            object[] _o_lchannel = new object[this.XmlRoot()["CONFIG"]["CHANNELS"].ChildNodes.Count];
            object[] _o_channel = new object[3];
            for (int i = 0; i < this.XmlRoot()["CONFIG"]["CHANNELS"].ChildNodes.Count; i++)
            {
                _o_channel[0] = (object)this.XmlRoot()["CONFIG"]["CHANNELS"].ChildNodes[i].Attributes["NAME"].Value;
                _o_channel[1] = (object)this.XmlRoot()["CONFIG"]["CHANNELS"].ChildNodes[i].Attributes["MODE"].Value;
                _o_channel[2] = (object)this.XmlRoot()["CONFIG"]["CHANNELS"].ChildNodes[i].Attributes["PERMISSION"].Value;

                _o_lchannel[i] = (object)_o_channel.Clone();
            }

            return _o_lchannel;

        }


        public string Permission(string ident)
        {
            string _s_permission = "";
            foreach (object _o_user in _o_users)
            {
                object[] _oo_user = (object[])_o_user;
                if (this.RegexpHostMask(ident, (string)_oo_user[1]))
                {
                    _s_permission = (string)_oo_user[4];
                    break;
                }

            }

            return _s_permission;
        }

        private bool RegexpHostMask(string ident, string hostmask)
        {
            bool result=false;
            Regex _r_temp = new Regex(hostmask.Replace("*",@"[\w._]*"));
            MatchCollection _matches = _r_temp.Matches(ident);
            if (_matches.Count != 0) result = true;
            return result;
        }

        public bool EnablePermission(string PluginPermission, string UserPermission)
        {
            bool enable = false;
            foreach (char _char in UserPermission)
            {
                if (PluginPermission.IndexOf(_char) != -1) enable = true;
            }
            return enable;
        }


        public string PermissionChannel(string channel)
        {
            string _s_permission = "";
            foreach (object _o_channel in _o_channels)
            {
                object[] _oo_channel = (object[])_o_channel;
                if (channel.ToLowerInvariant() == ((string)_oo_channel[0]).ToLowerInvariant())
                {
                    _s_permission = (string)_oo_channel[2];
                    break;
                }

            }
            return _s_permission;
        }


    }
}
